<?php

	/*This script adds a new project to the database with the user as its creator. 
	All project names are unique, thus the script echoes a message saying the name is taken if it so.
	A table for the project is created as well. The table has the same name as the project.
	In order to use it, you should supply it with a 'user' header in a POST request
	
	It ends by including get_projects.php. This has the affect of echoing html that
	represents the projects this user is currently a part of. This output can be 
	used to update the view by doing something similar to this in a callback function
	of a post:
	
		$("#user_projects").html(data);
	
	@method POST	
	@header user The name of the user that owns the new project
	@header project The name of the new project
	*/

	require_once("../functions/connect_to_database.php");
	
	//check to see if the project name exists
	$query = sprintf("SELECT name FROM projects WHERE name='%s'", mysql_real_escape_string($_POST['name']));
	$result = mysql_query($query);
	
	if(mysql_num_rows($result)){
		echo"<script> alert('The name \"".mysql_real_escape_string($_POST['name'])."\" is taken! Please use another name.');</script>";
	}
	
	else{
		$querries = array( sprintf("CREATE TABLE `%s` (
									  id int(30)unique NOT NULL auto_increment,
									  text varchar(500) NOT NULL,
									  name varchar(60) NOT NULL,
									  color enum('yellow','blue','green','yH','bH','gH') NOT NULL default 'yellow',
									  xyz varchar(20) NOT NULL,
									  points char(4) NOT NULL DEFAULT 0,
									  dt timestamp NOT NULL default CURRENT_TIMESTAMP,
									  PRIMARY KEY (id)
									);", 
									 mysql_real_escape_string($_POST['name'])),
									 
			sprintf("INSERT INTO projects(name,participant,isCreator) values('%s','%s',1);",
			        mysql_real_escape_string($_POST['name']),
			        mysql_real_escape_string($_POST['user']),1),
	
			sprintf("CREATE TABLE `%s_chat`(
					id int(11) UNIQUE NOT NULL auto_increment,
					username char(64) NOT NULL,
					message varchar(64) NOT NULL,
					color varchar(7) NULL,
					time TIMESTAMP DEFAULT CURRENT_TIMESTAMP);",
					mysql_real_escape_string($_POST['name']))
											 
		);
		
	
		for($i = 0; $i < count($querries); $i++){
			$result = mysql_query($querries[$i]);
		}
		echo"<script> alert('A project with the name \"".mysql_real_escape_string($_POST['name'])."\" was successfully created.');</script>";
		
		/* Insert into 'points' project name and id's of all seven divs*/
		
		//Initial div
		$query0 = sprintf("INSERT INTO `points` (projectName, divID) VALUES ('%s', 'note_start')",mysql_real_escape_string($_POST['name']));
		$result = mysql_query($query0);

		if(!$result){
			error_log($query.mysql_error());
			die(mysql_error());
		}

		
		//First div
		$query1 = sprintf("INSERT INTO `points` (projectName, divID) VALUES ('%s', 'one_point_main')",mysql_real_escape_string($_POST['name']));
		$result = mysql_query($query1);

		if(!$result){
			error_log($query.mysql_error());
			die(mysql_error());
		}

		
		//Second div
		$query2 = sprintf("INSERT INTO `points` (projectName, divID) VALUES ('%s', 'two_points_main')",mysql_real_escape_string($_POST['name']));
		$result = mysql_query($query2);

		if(!$result){
			error_log($query.mysql_error());
			die(mysql_error());
		}

		
		//Third div
		$query3 = sprintf("INSERT INTO `points` (projectName, divID) VALUES ('%s', 'three_points_main')",mysql_real_escape_string($_POST['name']));
		$result = mysql_query($query3);

		if(!$result){
			error_log($query.mysql_error());
			die(mysql_error());
		}

		
		//Fourth div
		$query4 = sprintf("INSERT INTO `points` (projectName, divID) VALUES ('%s', 'five_points_main')",mysql_real_escape_string($_POST['name']));
		$result = mysql_query($query4);

		if(!$result){
			error_log($query.mysql_error());
			die(mysql_error());
		}

		
		//Fifth div
		$query5 = sprintf("INSERT INTO `points` (projectName, divID) VALUES ('%s', 'eight_points_main')",mysql_real_escape_string($_POST['name']));
		$result = mysql_query($query5);

		if(!$result){
			error_log($query.mysql_error());
			die(mysql_error());
		}

		
		//Sixth div
		$query6 = sprintf("INSERT INTO `points` (projectName, divID) VALUES ('%s', 'thirteen_points_main')",mysql_real_escape_string($_POST['name']));
		$result = mysql_query($query6);

		if(!$result){
			error_log($query.mysql_error());
			die(mysql_error());
		}

		
	}
	include("get_projects.php");
?>
